cmake_minimum_required(VERSION 2.6)
PROJECT(nets)
ADD_DEFINITIONS(-Os -Wall -DGATEWAY_2_0 )

execute_process(COMMAND sed -n "s/^CONFIG_TARGET_ARCH_PACKAGES=\(.*\)/\1/p" ${TOPDIR}/.config
	OUTPUT_VARIABLE MY_ARCH_PACKAGES)

ADD_DEFINITIONS(-DGW_CHIPSET=${MY_ARCH_PACKAGES})

FILE(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)

SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)
#SET(SOURCES	*.c *.cpp)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} SOURCES)
set(CMAKE_EXE_LINKER_FLAGS "-static-libstdc++")

ADD_EXECUTABLE(nets ${SOURCES})

find_path(CAPI3_INCLUDE_DIR capi-app-mgr.h
	HINTS ${STAGING_DIR}/usr/include/libcapi3)

find_library(CAPI3_LIBRARY NAMES capi3
	HINTS ${STAGING_DIR}/usr/lib)

find_path(UBOX_INCLUDE_DIR libubox/ustream.h
	HINTS ${STAGING_DIR}/usr/include)
find_library(UBOX_LIBRARY NAMES ubox
	HINTS ${STAGING_DIR}/usr/lib)	

INCLUDE_DIRECTORIES(${CAPI3_INCLUDE_DIR} ${UBOX_INCLUDE_DIR})
TARGET_LINK_LIBRARIES(nets ${CAPI3_LIBRARY})

find_path(GLIB_INCLUDE_DIR glib.h
	HINTS ${STAGING_DIR}/usr/include/glib-2.0)

find_library(GLIB_LIBRARY NAMES glib-2.0
	HINTS ${PC_GLIB_LIBDIR} ${PC_GLIB_LIBRARY_DIRS})

find_path(GIO_INCLUDE_DIR gio/gio.h
	HINTS ${STAGING_DIR}/usr/include/glib-2.0)

find_library(GIO_LIBRARY NAMES gio-2.0
	HINTS ${PC_GIO_LIBDIR} ${PC_GIO_LIBRARY_DIRS})

find_library(GMODULE_LIBRARY NAMES gmodule-2.0
	HINTS ${PC_GMODULE_LIBDIR} ${PC_GMODULE_LIBRARY_DIRS})

find_path(GOBJECT_INCLUDE_DIR gio/gio.h
	HINTS ${STAGING_DIR}/usr/include/glib-2.0)

find_library(GOBJECT_LIBRARY NAMES gobject-2.0
	HINTS ${PC_GOBJECT_LIBDIR} ${PC_GOBJECT_LIBRARY_DIRS})
	
INCLUDE_DIRECTORIES(${GOBJECT_INCLUDE_DIR} ${GIO_INCLUDE_DIR} ${GLIB_INCLUDE_DIR} )
TARGET_LINK_LIBRARIES(nets ${GOBJECT_LIBRARY} ${GMODULE_LIBRARY} ${GIO_LIBRARY} ${GLIB_LIBRARY})


find_library(FFI_LIBRARY NAMES ffi
	HINTS ${PC_FFI_LIBDIR} ${PC_FFI_LIBRARY_DIRS})

find_library(Z_LIBRARY NAMES z
	HINTS ${PC_Z_LIBDIR} ${PC_Z_LIBRARY_DIRS})

if(${MY_ARCH_PACKAGES} MATCHES "zx279127")
	TARGET_LINK_LIBRARIES(nets z ${FFI_LIBRARY} ${CAP_LIBRARY} ${Z_LIBRARY} ${UBOX_LIBRARY} pthread rt)
elseif(${MY_ARCH_PACKAGES} MATCHES "zx279128")
	TARGET_LINK_LIBRARIES(nets z ${FFI_LIBRARY} ${CAP_LIBRARY} ${Z_LIBRARY} ${UBOX_LIBRARY} pthread rt)
elseif(${MY_ARCH_PACKAGES} MATCHES "zx279121")
	TARGET_LINK_LIBRARIES(nets ${FFI_LIBRARY} ${CAP_LIBRARY} ${Z_LIBRARY} ${UBOX_LIBRARY} pthread rt)
elseif(${MY_ARCH_PACKAGES} MATCHES "zx2791")
	TARGET_LINK_LIBRARIES(nets z ${FFI_LIBRARY} ${CAP_LIBRARY} ${Z_LIBRARY} ${UBOX_LIBRARY} pthread rt)
elseif(${MY_ARCH_PACKAGES} MATCHES "zxic127")
	TARGET_LINK_LIBRARIES(nets z ${FFI_LIBRARY} ${CAP_LIBRARY} ${Z_LIBRARY} ${UBOX_LIBRARY} pthread rt)
elseif(${MY_ARCH_PACKAGES} MATCHES "zxic128")
	TARGET_LINK_LIBRARIES(nets z ${FFI_LIBRARY} ${CAP_LIBRARY} ${Z_LIBRARY} ${UBOX_LIBRARY} pthread rt)
else()
	TARGET_LINK_LIBRARIES(nets ${FFI_LIBRARY} ${CAP_LIBRARY} ${Z_LIBRARY} ${UBOX_LIBRARY} pthread rt)
endif()


INSTALL(TARGETS nets RUNTIME DESTINATION bin)
